package com.sun.demo2.controller

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.sun.demo2.moudel.HttpResult
import com.sun.demo2.moudel.Movie
import com.sun.demo2.service.MyService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*

@Controller
class HelloController {
    @Autowired
    var myService: MyService? = null

    @ResponseBody
    @RequestMapping("/hello")
    fun hello(): String {

        return "hello word quick"
    }

    @ResponseBody
    @GetMapping("/page")
    fun getPage(page: Page<Movie>, @RequestBody movie: Movie?): HttpResult<MutableList<Movie>> {
        val myPage = myService?.page(page, QueryWrapper(movie))

//        var name = movie?.name
//        if(name == null){
//            throw RuntimeException()
//        }
        return if (myPage != null) {

             HttpResult(200, "成功", myPage.records, myPage.records.size, myPage.total)
         } else {
            HttpResult(400, "失败", myPage?.records, null, null)
         }
    }
    @ResponseBody
    @GetMapping("/pageList")
    fun getPageList(page: Page<Movie>, @RequestBody movie: Movie?): MutableList<Movie>? {
        val myPage = myService?.page(page, QueryWrapper(movie))

//        var name = movie?.name
//        if(name == null){
//            throw RuntimeException()
//        }
        return if (myPage?.records.isNullOrEmpty()){
            mutableListOf()
        }else {
            myPage?.records
        }
    }

    @ResponseBody
    @GetMapping("/query")
    fun getPageList(@RequestBody movie: Movie?): HttpResult<MutableList<Movie>> {
        val myPage = myService?.list()

//        var name = movie?.name
//        if(name == null){
//            throw RuntimeException()
//        }
        val httpResult = HttpResult(200,"成功",myPage)

        return httpResult
    }

    @ResponseBody
    @GetMapping("/add")
    fun addPage(@RequestBody movie: Movie?): HttpResult<Boolean> {
        val myPage = myService?.save(movie)
//
//        var name = movie?.name
//        if(name == null){
//            throw RuntimeException()
//        }
        val httpResult = HttpResult<Boolean>(200,"成功",myPage)

        return httpResult
    }

    @ResponseBody
    @DeleteMapping("/delete")
    fun deletePage(@RequestBody movie: Movie): HttpResult<Boolean> {
        val myPage = myService?.removeById(movie.id)
//
//        var name = movie?.name
//        if(name == null){
//            throw RuntimeException()
//        }
        val httpResult = HttpResult<Boolean>(200,"成功",myPage)

        return httpResult
    }


}